split and join¶

🎨 split¶

In [1]:
'Here are some words'.split()
Out[1]:
['Here', 'are', 'some', 'words']
In [2]:
for word in 'here are some words'.split():
    print(word)
here
are
some
words
In [3]:
print('Do  you     know  \n      the     \t          muffin      \n              man?'.split())
['Do', 'you', 'know', 'the', 'muffin', 'man?']

cats.py 🐈 🐈‍⬛¶

🎨 join¶

In [4]:
' '.join(['red', 'fish', 'blue', 'fish'])
Out[4]:
'red fish blue fish'
In [5]:
" :) ".join(['red', 'fish', 'blue', 'fish'])
Out[5]:
'red :) fish :) blue :) fish'
In [8]:
seuss = ['red', 'fish', 'blue', 'fish']
fish = '🐟'
fish.join(seuss)
Out[8]:
'red🐟fish🐟blue🐟fish'

cougars.py¶

CSV - Comma Separated Values¶

In [11]:
'House,Favorite place,School,Major'.split(',')
Out[11]:
['House', 'Favorite place', 'School', 'Major']
In [14]:
'white,BYU,BYU,Computer Science'.split(',')
Out[14]:
['white', 'BYU', 'BYU', 'Computer Science']

👨🏻‍🎨 unemployment.py¶

The file unemployment.txt looks like this:

          State/Area  Year  Month  %_Unemployed
             Alabama  1976      1           6.6
              Alaska  1976      1           7.1
             Arizona  1976      1          10.2
            Arkansas  1976      1           7.3
          California  1976      1           9.2
  Los-Angeles-County  1976      1           8.9

Write a program that takes a file like unemployment.txt and a state and prints the maximum unemployment observed for that state.

What are the different list patterns you might use?

The file unemployment.txt looks like this:

          State/Area  Year  Month  %_Unemployed
             Alabama  1976      1           6.6
              Alaska  1976      1           7.1
             Arizona  1976      1          10.2
            Arkansas  1976      1           7.3
          California  1976      1           9.2
  Los-Angeles-County  1976      1           8.9

Write a program that takes a file like unemployment.txt and a year and prints the average unemployment observed for that year.

What are the different list patterns you might use?

What other questions can YOU answer with this data?

Key Ideas¶

  • split
    • default: all whitespace
    • or give it a substring to split on
  • join
  • Tabular data
    • Each line is a list of elements